#include<stdio.h>
#include<iostream>
#include<string>
#include <algorithm>
using namespace std;
struct student
{
	string name;
	int all;
	int chi;
	int mat;
}stu[1000];
bool cmp(const student a, const student b)
{
	if (a.all != b.all)return a.all > b.all;
	if (a.chi != b.chi)return a.chi > b.chi;
	return a.mat > b.mat;
}
int main()
{
	int n,i;
	cin >> n;
	for (i = 0; i < n; i++)cin >> stu[i].name >> stu[i].all >> stu[i].chi >> stu[i].mat;
	sort(stu, stu + n, cmp);
	for (i = 0; i < n; i++)cout << stu[i].name << endl;
	return 0;
}